<?php
/**
 * Created by PhpStorm.
 * User: MRPHAN
 * Date: 9/21/2014
 * Time: 10:31 PM
 */
namespace Common\Models\Videos;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;
use Phalcon\Mvc\Model\Resultset\Simple;

class Artist extends Model
{
    const ACTOR = 1;
    const DIRECTOR = 2;

    protected $id;
    protected $artist_type;
    protected $name;
    protected $national_id;
    protected $gender;
    protected $info;

    public static $table = 'artists';


    public function getSource()
    {
        return 'artists';
    }
    
    public function initialize()
    {
        $this->setSource('artists');
        
        $this->belongsTo('national_id','Common\Models\Videos\National','id',array(
            'alias'     =>  'national'
        ));
        
        $this->hasMany('id' , 'Common\Models\VideoArtistLink' , 'artist_id' , array(
            'alias' => 'videoLink',
            'foreignKey' => array(
                'message' => 'artists cannot be deleted because it\'s used on videoLink'
            )
        ));

    }

    public function beforeDelete()
    {
        $videoFileLink = VideoArtistLink::find(array(
            "artist_id = " . $this->id
        ));
        if($videoFileLink)
        {
            $videoFileLink->delete();
        }
    }

    /**
     * @return mixed
     */
    public function getGender()
    {
        return $this->gender;
    }

    /**
     * @return mixed
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getInfo()
    {
        return $this->info;
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return mixed
     */
    public function getNationalId()
    {
        return $this->national_id;
    }

    /**
     * @return mixed
     */
    public function getArtistType()
    {
        return $this->artist_type;
    }



    /**
     * @param mixed $gender
     */
    public function setGender($gender)
    {
        $this->gender = $gender;
    }

    /**
     * @param mixed $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @param mixed $info
     */
    public function setInfo($info)
    {
        $this->info = $info;
    }

    /**
     * @param mixed $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @param mixed $national_id
     */
    public function setNationalId($national_id)
    {
        $this->national_id = $national_id;
    }

    /**
     * @param mixed $artist_type
     */
    public function setArtistType($artist_type)
    {
        $this->artist_type = $artist_type;
    }

    public function queryBuilder($conditions)
    {
        $sqlWhere = '1 = 1';
        $conditionsWhere = array();

        if(isset($conditions['id']) && $conditions['id'])
        {
            $sqlWhere .= ' AND artist.id = :id:';
            $conditionsWhere['id'] = $conditions['id'];
        }

        if(isset($conditions['artist_type']) && $conditions['artist_type'])
        {
            $sqlWhere .= ' AND artist.artist_type = :artist_type:';
            $conditionsWhere['artist_type'] = $conditions['artist_type'];
        }

        if(isset($conditions['name']) && $conditions['name'])
        {
            $sqlWhere .= ' AND artist.name LIKE :name:';
            $conditionsWhere['name'] = '%' . $conditions['name'] . '%';
        }

        if(isset($conditions['national_id']) && $conditions['national_id'])
        {
            $sqlWhere .= ' AND artist.national_id = :national_id:';
            $conditionsWhere['national_id'] = $conditions['national_id'];
        }

        if(isset($conditions['gender']) && $conditions['gender'])
        {
            $sqlWhere .= ' AND artist.gender = :gender:';
            $conditionsWhere['gender'] = $conditions['gender'];
        }            

        $artistBuilder = $this->getDI()->getModelsManager()->createBuilder()
            ->from(array('artist' => '\Backend\Modules\Video\Models\Artist'))
            ->groupBy("artist.id")
            ->where($sqlWhere , $conditionsWhere)
            ->orderBy('artist.id');
        return $artistBuilder;
    }

}